package cn.yhjz.admin.biz.controller;

import cn.yhjz.admin.biz.service.BizProductService;
import cn.yhjz.admin.biz.entity.BizProduct;
import cn.yhjz.admin.biz.vo.BizProductVo;
import cn.yhjz.common.AjaxResponse;
import cn.yhjz.platform.system.util.LoginUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/biz/product")
public class BizProductController {

    @Autowired
    private BizProductService bizProductService;

    @PostMapping("/list")
    public List<BizProductVo> list(@RequestBody BizProduct bizProduct) {
        return bizProductService.select(bizProduct);
    }

    @PostMapping("/page")
    public AjaxResponse page(@RequestBody BizProductVo vo) {
        BizProduct entity = new BizProduct();
        BeanUtils.copyProperties(vo, entity);
        return AjaxResponse.success(bizProductService.getPage(vo.getCurrentPage(), vo.getPageSize(), entity));
    }

    @PostMapping("/addOrUpdate")
    public AjaxResponse add(@RequestBody BizProduct entity) {
        if (entity.getId() != null) {
            entity.setUpdatedBy(LoginUtil.getCurrentUser().getUserCode());
            entity.setUpdatedTime(new Date());
            return AjaxResponse.success(bizProductService.updateById(entity));
        } else {
            entity.setCreatedBy(LoginUtil.getCurrentUser().getUserCode());
            entity.setCreatedTime(new Date());
            return AjaxResponse.success(bizProductService.insert(entity));
        }
    }

    @GetMapping("/selectById")
    public AjaxResponse selectById(Long id) {
        return AjaxResponse.success(bizProductService.selectById(id));
    }

    @DeleteMapping("/deleteById")
    public AjaxResponse deleteById(Long id) {
        return AjaxResponse.success(bizProductService.deleteById(id));
    }
}
